package com.office.web.controller.system;

import java.util.List;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.office.common.annotation.Log;
import com.office.common.core.controller.BaseController;
import com.office.common.core.domain.AjaxResult;
import com.office.common.enums.BusinessType;
import com.office.system.domain.SysReportTemplate;
import com.office.system.service.ISysReportTemplateService;
import com.office.common.utils.poi.ExcelUtil;
import com.office.common.core.page.TableDataInfo;

/**
 * 报告模板Controller
 */
@RestController
@RequestMapping("/system/template")
public class SysReportTemplateController extends BaseController {
    @Autowired
    private ISysReportTemplateService sysReportTemplateService;

    /**
     * 查询报告模板列表
     */
    @PreAuthorize("@ss.hasPermi('system:template:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysReportTemplate sysReportTemplate) {
        startPage();
        List<SysReportTemplate> list = sysReportTemplateService.selectSysReportTemplateList(sysReportTemplate);
        return getDataTable(list);
    }

    /**
     * 导出报告模板列表
     */
    @PreAuthorize("@ss.hasPermi('system:template:export')")
    @Log(title = "报告模板", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysReportTemplate sysReportTemplate) {
        List<SysReportTemplate> list = sysReportTemplateService.selectSysReportTemplateList(sysReportTemplate);
        ExcelUtil<SysReportTemplate> util = new ExcelUtil<SysReportTemplate>(SysReportTemplate.class);
        util.exportExcel(response, list, "报告模板数据");
    }

    /**
     * 获取报告模板详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:template:query')")
    @GetMapping(value = "/{templateId}")
    public AjaxResult getInfo(@PathVariable("templateId") Long templateId) {
        return success(sysReportTemplateService.selectSysReportTemplateByTemplateId(templateId));
    }

    /**
     * 新增报告模板
     */
    @PreAuthorize("@ss.hasPermi('system:template:add')")
    @Log(title = "报告模板", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysReportTemplate sysReportTemplate) {
        return toAjax(sysReportTemplateService.insertSysReportTemplate(sysReportTemplate));
    }

    /**
     * 修改报告模板
     */
    @PreAuthorize("@ss.hasPermi('system:template:edit')")
    @Log(title = "报告模板", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysReportTemplate sysReportTemplate) {
        return toAjax(sysReportTemplateService.updateSysReportTemplate(sysReportTemplate));
    }

    /**
     * 删除报告模板
     */
    @PreAuthorize("@ss.hasPermi('system:template:remove')")
    @Log(title = "报告模板", businessType = BusinessType.DELETE)
    @DeleteMapping("/{templateIds}")
    public AjaxResult remove(@PathVariable Long[] templateIds) {
        return toAjax(sysReportTemplateService.deleteSysReportTemplateByTemplateIds(templateIds));
    }

    /**
     * 导入报告模板
     */
    @PreAuthorize("@ss.hasPermi('system:template:import')")
    @Log(title = "导入报告模板", businessType = BusinessType.IMPORT)
    @PostMapping("/import")
    public AjaxResult importTemplate(@RequestBody SysReportTemplate template) {
        try {
            // 这里实现模板导入逻辑
            return toAjax(sysReportTemplateService.insertSysReportTemplate(template));
        } catch (Exception e) {
            return error("导入模板失败：" + e.getMessage());
        }
    }

    /**
     * 导出单个报告模板
     */
    @PreAuthorize("@ss.hasPermi('system:template:export')")
    @GetMapping("/export/{templateId}")
    public AjaxResult exportTemplate(@PathVariable Long templateId) {
        try {
            SysReportTemplate template = sysReportTemplateService.selectSysReportTemplateByTemplateId(templateId);
            if (template == null) {
                return error("模板不存在");
            }
            return success(template);
        } catch (Exception e) {
            return error("导出模板失败：" + e.getMessage());
        }
    }

    /**
     * 获取报告模板预览
     */
    @GetMapping("/preview/{templateId}")
    public AjaxResult getTemplatePreview(@PathVariable Long templateId) {
        try {
            SysReportTemplate template = sysReportTemplateService.selectSysReportTemplateByTemplateId(templateId);
            if (template == null) {
                return error("模板不存在");
            }
            return success(template);
        } catch (Exception e) {
            return error("获取模板预览失败：" + e.getMessage());
        }
    }
}
